# coding=utf-8

'''
-数据准备
    .为驱动层提供可靠数据
'''
from configparser import ConfigParser
from my_test.common.keylib import Keylib
from my_test.common.deal_data import Deal_data
from my_test.tools.tools import *


class Readyer:

    def __init__(self):
        self.conf = ConfigParser()
        self.conf.read(pro_path() + '/conf.ini', encoding='utf-8')
        self.CaseDataDir = self.conf.get('main', 'CaseDataDir')
        self.cases = Deal_data().get_case(pro_path() + self.CaseDataDir)
        self.keylib = Keylib()

    def ready(self):
        data = []
        for case in self.cases:
            item = []
            method = case.method
            try:
                if case.flag == '1':
                    if case.prepose == '':
                        # if hasattr(self.keylib, method):
                        fuc = getattr(self.keylib, method)
                        item.append([fuc,case.exp,case.flag])
                        item.extend([case.url,case.headers,case.body])
                        data.append(item)
                    else:
                        for preposecase in self.cases:
                            if case.prepose == preposecase.id:
                                preposefuc = getattr(self.keylib, preposecase.method)
                                item.append([preposefuc,preposecase.exp,preposecase.flag])
                                item.append([preposecase.url,preposecase.headers,preposecase.body])
                                break
                        fuc = getattr(self.keylib, method)
                        item.append([fuc,case.exp,case.flag])
                        item.append([case.url,case.headers,case.body])
                        data.append(item)
                else:
                    item.append([case.method,case.exp,case.flag])
                    item.extend([case.url,case.headers,case.body])
                    data.append(item)
                    # return method
            except:
                item.append([case.method,case.exp,case.flag])
                item.extend([case.url,case.headers,case.body])
                data.append(item)
                print(f'id为: {case.id} 的用例，关键字库中没有 {method} 方法')
        return data
